package fr.rinie.android_first_step;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import java.util.List;

/**
 * All copyright reserved.
 * User: romain
 * Date: 11/16/13
 * Time: 1:30 PM
 * Please contact romain.rinie@xcom.de before any modification/distribution.
 */
public class DisplayFishDetails extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.fishdetails);

        Intent intent=this.getIntent();
        Bundle bundle=intent.getExtras();

        FishContainer.Fish fish =
                (FishContainer.Fish)bundle.getSerializable("value");
        TextView scientificName = (TextView) findViewById(R.id.name);
        scientificName.setText(fish.scientificName);
        TextView commonName= (TextView) findViewById(R.id.commonName);
        commonName.setText(buildCommonNameEntry(fish));
        TextView describer = (TextView) findViewById(R.id.descripteur);
        describer.setText(fish.describer);
        TextView family = (TextView) findViewById(R.id.family);
        family.setText(fish.family);
        TextView size = (TextView) findViewById(R.id.size);
        size.setText(buildSizeEntry(fish));
        TextView origin = (TextView) findViewById(R.id.origin);
        origin.setText(fish.origin);
        TextView temperature = (TextView) findViewById(R.id.temperature);
        temperature.setText(buildTempEntry(fish));
        TextView lifeZone = (TextView) findViewById(R.id.livingZone);
        lifeZone.setText(fish.lifeZone);
        TextView description = (TextView) findViewById(R.id.description);
        description.setText(fish.description);
        TextView reproduction = (TextView) findViewById(R.id.reproduction);
        reproduction.setText(fish.reproduction);
        TextView dimorphism = (TextView) findViewById(R.id.dismorphisme);
        dimorphism.setText(fish.dimorphism);
        TextView acidity = (TextView) findViewById(R.id.pH);
        acidity.setText(buildPhEntry(fish));
        TextView lifeExpectancy = (TextView) findViewById(R.id.lifeExpectancy);
        lifeExpectancy.setText(fish.lifeExpectancy);
        TextView behaviour = (TextView) findViewById(R.id.behaviour);
        behaviour.setText(fish.behaviour);
    }

    private String buildPhEntry(final FishContainer.Fish fish) {
        FishContainer.Fish.Acidity acidity = fish.acidity;
        if (null == acidity) {
            return "Pas d'information disponible";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(acidity.phMin);
        sb.append(" °d GH à ");
        sb.append(acidity.phMax);
        sb.append(" °d GH.");
        if (null != acidity.phRepro && !acidity.phRepro.toString().equals("0.0")) {
            sb.append(" En période de reproduction: ");
            sb.append(acidity.phRepro);
            sb.append(" °d GH.");
        }
        return sb.toString();
    }

    private String buildTempEntry(final FishContainer.Fish fish) {
        FishContainer.Fish.Temperature temp = fish.temperature;
        if (null == temp) {
            return "Pas d'information disponible";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(temp.tempMin);
        sb.append(" °C à ");
        sb.append(temp.tempMax);
        sb.append(" °C.");
        if (null != temp.tempRepro && !temp.tempRepro.toString().equals("0")) {
            sb.append(" En période de reproduction: ");
            sb.append(temp.tempRepro);
            sb.append(" °C.");
        }
        return sb.toString();
    }

    private String buildSizeEntry(final FishContainer.Fish fish) {
        if (null == fish.size) {
            return "Pas d'information disponible";
        }
        StringBuilder sb = new StringBuilder("Mâle: ");
        sb.append(fish.size.malSize);
        sb.append(" cm, Femelle: ");
        sb.append(fish.size.femaleSize);
        sb.append(" cm.");
        return sb.toString();
    }

    private String buildCommonNameEntry(final FishContainer.Fish fish) {
        List<String> commonNameList = fish.commonName;
        if (commonNameList.isEmpty()) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (String current : commonNameList) {
            sb.append(current);
            sb.append(", ");
        }
        String commonName = sb.toString();
        commonName = commonName.substring(0, commonName.length() - 2);
        commonName = commonName + ".";
        return commonName;
    }
}